<?php

namespace app\model\goods;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-GoodsNavbarModel
 * Class GoodsNavbarModel
 * @package app\model
 */
class GoodsNavbarModel extends BaseModel
{
    use SoftDelete;
    protected $name = 'goods_navbar';

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('name', "%$keyword%");
        }
    }

    /**
     * 获取分页列表
     * @param $map
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getPageData($map, $keyword = "", $current_page, $list_rows)
    {
        $dataList = $this
            ->keyword($keyword)
            ->where($map)
            ->order('sort', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 获取列表
     * @param $order
     * @param $field
     * @param $is_member
     * @return mixed
     */
    public function getNavList($order, $field, $is_member = 1)
    {
        $list = $this->alias('a')
            ->join('t_goods g', 'g.goods_nav_id=a.id')
            ->join("t_goods_type c", "g.goods_type_id=c.id")
            ->where('g.goods_status', 1)
            ->where('g.is_member', '<=', $is_member)
            ->where('a.status', 0)
            ->where('c.status', 0)
            ->group('a.id')
            ->order($order)->field($field)->select();
        $list_arr = [];
        foreach ($list as $value) {
            if (GoodsModel::isHasGoods($value['id'], $is_member)) {
                $list_arr[] = $value;
            }
        }
        return $list_arr;
    }

    /**
     * 获取活动商品导航列表
     * @param $activity_id
     * @param $order
     * @param $field
     * @param $is_member
     * @return mixed
     */
    public function getActivityNavList($activity_id = 1, $order, $field, $is_member = 1)
    {
        $list = $this->alias('a')
            ->leftJoin('t_goods g', 'g.goods_nav_id=a.id')
            ->leftJoin('t_activity_goods c', 'c.goods_id=g.id')
            ->leftJoin('t_activity t', 't.id=c.activity_id')
            ->leftJoin('t_goods_common s', 's.attribute_ids=c.attribute_ids')
            ->where('c.activity_id', $activity_id)
            ->where('g.goods_status', 1)
            ->where('g.is_member', '<=', $is_member)
            ->where('a.status', 0)
            ->where('c.status', 1)
            ->group('g.goods_nav_id')
            ->order($order)->field($field)->select();
        return $list;
    }
}
